package com.itheima.service.impl;

import com.itheima.Utils.JwtUtils;
import com.itheima.mapper.LoginMapper;
import com.itheima.pojo.Login;
import com.itheima.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;

@Service
public class LoginServicelmpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;

    @Override
    public Login login(Login login) {
        //查询数据库
        Login log = loginMapper.findUser(login.getUsername(),login.getPassword());
        //判断返回数据是否为空
        if (ObjectUtils.isEmpty(log)) {
            //为空
            throw new RuntimeException("用户名或密码错误！");
        }
        //返回不为空 生成token
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("id", log.getId());
        objectObjectHashMap.put("username", log.getUsername());
        String jwt = JwtUtils.generateJwt(objectObjectHashMap);
        //赋值给logint
        Login login1 = new Login(log.getId(), log.getUsername(), log.getPassword(), jwt);

        return login1;
    }
}
